using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Flujo.Actividades;
using ConexionBBDD.Actividades;

namespace Interfaz.Actividades
{
    public partial class RegistroReservas : Form
    {
        private ControlYGestion.Eleccion anterior = null;
        private GestionReservas gestReserva;
        private String filtroSQL = "";

        public RegistroReservas(ControlYGestion.Eleccion e)
        {
            anterior = e;
            InitializeComponent();

            this.gestReserva = new GestionReservas();

            this.actualizarGridView();
            this.configurarFiltrar();
            this.actualizarEstadisticas();
        }

        private void configurarFiltrar()
        {
            this.fecha.NullValue = "<Seleccionar fecha>";
            this.fecha.Value = null;
            this.filtro.Text = "Todas";
        }

        private void aniadir_Click(object sender, EventArgs e)
        {
            Actividades.DialogoReserva dialogo_reserva = new Actividades.DialogoReserva();
            dialogo_reserva.ShowDialog();

            Reserva reserva = dialogo_reserva.getReserva();
            if (reserva != null)
                this.gestReserva.addReserva(reserva);
            this.actualizarEstadisticas();
            this.actualizarBotones();
        }

        private void actualizarGridView()
        {
            BindingSource bsfoo = gestReserva.listarReservas(filtroSQL);
            this.reservaGridView.DataSource = bsfoo;
            //this.reservaGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

            this.reservaGridView.Columns[0].HeaderText = "ID";
            this.reservaGridView.Columns[0].Visible = false;
            this.reservaGridView.Columns[1].HeaderText = "Nombre";
            this.reservaGridView.Columns[1].DisplayIndex = 1;
            this.reservaGridView.Columns[2].HeaderText = "Fecha";
            this.reservaGridView.Columns[2].DisplayIndex = 2;
            this.reservaGridView.Columns[3].HeaderText = "NumeroVisitas";
            this.reservaGridView.Columns[3].DisplayIndex = 3;

            this.actualizarBotones();           
        }

        private void actualizarBotones()
        {
            BindingSource bsfoo = gestReserva.listarReservas(filtroSQL);
            if (bsfoo.Count == 0)
            {
                this.eliminar.Enabled = false;
                this.confirmarReserva.Enabled = false;
            }
            else
            {
                this.eliminar.Enabled = true;
                this.confirmarReserva.Enabled = true;
            }
        }

        private void actualizarEstadisticas()
        {
            //Actualizamos las estadisticas
            this.reservaTotales.Text = this.gestReserva.getEstadisticas().ToString();
            this.reservaConfirmada.Text = this.gestReserva.getConfirmadas().ToString();
            this.reservaPendientes.Text = this.gestReserva.getPendientes().ToString();
        }

        private void eliminar_Click(object sender, EventArgs e)
        {
            Int32 index = (Int32)this.reservaGridView.CurrentRow.Cells[0].Value;
            this.gestReserva.deleteReserva(index);
            this.actualizarBotones();
            this.actualizarEstadisticas();
        }

        private void reservaGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            this.actualizarGridView();
            this.actualizarEstadisticas();
        }

        private void reservaGridView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            this.actualizarGridView();
            this.actualizarEstadisticas();
        }



        private void volver_Click(object sender, EventArgs e)
        {
            anterior.Show();
            this.Close();
        }

        private void filtrar_Click(object sender, EventArgs e)
        {
            String fc_fecha = "";
            if (this.fecha.Value != null)
            {
                DateTime dia_siguiente = ((DateTime)this.fecha.Value).Date;
                DateTime dia = dia_siguiente.AddDays(-1);

                fc_fecha = String.Format("(fechaVisita >= '{0}') AND (fechaVisita <= '{1}')",
                    dia.ToString("#yyyy/MM/dd hh:mm#"), dia_siguiente.ToString("#yyyy/MM/dd hh:mm#"));
            }
            else
            {
                fc_fecha = "(1 = 1)";
            }

            String fc_filtro = "";
            switch (this.filtro.Text)
            {
                case "Todas":
                    fc_filtro = "";
                    break;
                case "Pendientes":
                    fc_filtro = "";
                    break;
                case "Confirmadas":
                    //fc_filtro = " AND (estado = false)";
                    fc_filtro = "";
                    break;
            }
 
            String fc_buscar = "";
            if (this.nombreReserva.Text != "")
            {
                fc_buscar = " AND (nombreVisitante LIKE '*" + this.nombreReserva.Text + "*')";
            }

            this.filtroSQL = fc_fecha + fc_filtro + fc_buscar;

            actualizarGridView();
            actualizarEstadisticas();
        }

        private void restablecer_Click(object sender, EventArgs e)
        {
            this.filtroSQL = "";

            actualizarGridView();
        }
    }
}